//
//  JobDetailBaseInfoView.m
//  Btpin
//
//  Created by Vic on 2019/3/20.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "JobDetailBaseInfoView.h"
#import "CompanyTagsView.h"
#import "CompanyContentView.h"
#import "UIImage+Helper.h"

@interface JobDetailBaseInfoView ()<CompanyContentViewDelegate>
{
    
}
@end

@implementation JobDetailBaseInfoView

- (instancetype)initWithFrame:(CGRect)frame target:(id)target info:(nonnull CompanyJobModel *)model showRightIcon:(BOOL)showFlag topBgColor:(nonnull UIColor *)bgColor {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUIWithInfo:model showRightIcon:showFlag topBgColor:bgColor];
        self.delegate = target;
    }
    return self;
}
- (void)initUIWithInfo:(nonnull CompanyJobModel *)model showRightIcon:(BOOL)showFlag topBgColor:(nonnull UIColor *)bgColor {
    self.backgroundColor = COLOR_OF_WHITE;
    
    self.shareBgView = [UIToolsControl initViewWithFrame:CGRectMake(0, 0, WIDTH, 100) backgroundColor:COLOR_OF_WHITE];
    [self addSubview:self.shareBgView];
    
    UIView *topBgView = [UIToolsControl initViewWithFrame:CGRectMake(ScalePS(15), ScalePS(10), WIDTH-ScalePS(30), ScalePS(160)) backgroundColor:bgColor];
    [self.shareBgView addSubview:topBgView];
    topBgView.userInteractionEnabled = YES;
    topBgView.layer.cornerRadius = ScalePS(10);
    topBgView.layer.masksToBounds = YES;
    
    UILabel *jobLbl = [UIToolsControl initLabelWithFrame:CGRectMake(LeftSpace, 15*Scale, ViewWidth(topBgView)-115*Scale, 35*Scale) text:model.title textColor:COLOR_OF_WHITE font:Font_22];
    jobLbl.font = FontBold(Font_22);
    [topBgView addSubview:jobLbl];
    
//    UILabel *baseInfoLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(jobLbl), EndY(jobLbl), ViewWidth(jobLbl), 25*Scale) text:[NSString stringWithFormat:@"%@ | %@ | %@", model.district_cn, model.experience_cn, model.education_cn] textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
//    [topBgView addSubview:baseInfoLbl];
    
    CGFloat addrWidth = [UIToolsControl textWidth:model.district_cn size:CGSizeMake(MAXFLOAT, ScalePS(25)) font:Font_13]+ScalePS(20);
    UILabel *addrLbl = [UIToolsControl initLabelWithFrame:CGRectMake(LeftSpace, EndY(jobLbl), addrWidth, ScalePS(25)) text:model.district_cn textColor:COLOR_OF_WHITE font:Font_13];
    addrLbl.textAlignment = NSTextAlignmentCenter;
    addrLbl.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    addrLbl.layer.cornerRadius = ScalePS(12.5);
    addrLbl.layer.masksToBounds = YES;
    [topBgView addSubview:addrLbl];
    
    CGFloat expWidth = [UIToolsControl textWidth:model.experience_cn size:CGSizeMake(MAXFLOAT, ScalePS(25)) font:Font_13]+ScalePS(20);
    UILabel *expLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(addrLbl)+ScalePS(5), EndY(jobLbl), expWidth, ScalePS(25)) text:model.experience_cn textColor:COLOR_OF_WHITE font:Font_13];
    expLbl.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    expLbl.textAlignment = NSTextAlignmentCenter;
    expLbl.layer.cornerRadius = ScalePS(12.5);
    expLbl.layer.masksToBounds = YES;
    [topBgView addSubview:expLbl];
    
    CGFloat eduWidth = [UIToolsControl textWidth:model.education_cn size:CGSizeMake(MAXFLOAT, ScalePS(25)) font:Font_13]+ScalePS(20);
    UILabel *eduLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(expLbl)+ScalePS(5), EndY(jobLbl), eduWidth, ScalePS(25)) text:model.education_cn textColor:COLOR_OF_WHITE font:Font_13];
    eduLbl.textAlignment = NSTextAlignmentCenter;
    eduLbl.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    eduLbl.layer.cornerRadius = ScalePS(12.5);
    eduLbl.layer.masksToBounds = YES;
    [topBgView addSubview:eduLbl];
    
    UILabel *wageLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(jobLbl), StartY(jobLbl), 85*Scale, ViewHeight(jobLbl)) text:model.wage_cn textColor:COLOR_OF_WHITE font:Font_15];
    wageLbl.font = FontBold(Font_15);
    wageLbl.textAlignment = NSTextAlignmentRight;
    [topBgView addSubview:wageLbl];
    
    UIView *hrBgView = [UIToolsControl initViewWithFrame:CGRectMake(LeftSpace, EndY(addrLbl)+15*Scale, ViewWidth(topBgView)-30*Scale, 65*Scale) backgroundColor:COLOR_OF_CLEAR];
    [topBgView addSubview:hrBgView];
    UIImageView *hrHeaderImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(0, 0, 52*Scale, 52*Scale) imageName:@"header_bg"];
    hrHeaderImgView.contentMode = UIViewContentModeScaleAspectFill;
    hrHeaderImgView.layer.cornerRadius = 26*Scale;
    hrHeaderImgView.layer.masksToBounds = YES;
    hrHeaderImgView.backgroundColor = THEMECOLOR_OF_GRAY;
    [hrBgView addSubview:hrHeaderImgView];
    if (model.hrInfoModel.avatar && model.hrInfoModel.avatar.length) {
        [hrHeaderImgView sd_setImageWithURL:[NSURL URLWithString:model.hrInfoModel.avatar]];
    }
    CGFloat hrNameWidth = [UIToolsControl textWidth:model.hrInfoModel.name size:CGSizeMake(MAXFLOAT, 25*Scale) font:Font_15]+5*Scale;
    UILabel *hrNameLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(hrHeaderImgView)+15*Scale, 5*Scale, hrNameWidth, 25*Scale) text:model.hrInfoModel.name textColor:COLOR_OF_WHITE font:Font_15];
    [hrBgView addSubview:hrNameLbl];
    
    UILabel *updateLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(hrNameLbl)+ScalePS(5), 5*Scale, 200, ViewHeight(hrNameLbl)) text:model.hrInfoModel.last_timeShow textColor:COLOR_OF_WHITE font:Font_12];
    [hrBgView addSubview:updateLbl];
    
    NSString *companyName = model.companyInfoModel.name;
    if (model.companyInfoModel.name && (model.companyInfoModel.name.length > 10)) {
        companyName = [model.companyInfoModel.name substringToIndex:10];
        companyName = [NSString stringWithFormat:@"%@...", companyName];
    }
    UILabel *hrBaseInfoLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(hrNameLbl), EndY(hrNameLbl), 300, 25*Scale) text:[NSString stringWithFormat:@"%@·%@", companyName, model.hrInfoModel.position] textColor:COLOR_OF_WHITE font:Font_13];
    [hrBgView addSubview:hrBaseInfoLbl];
    if (showFlag) {
        UIImageView *rightImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(ViewWidth(hrBgView)-10*Scale, ((ViewHeight(hrBgView)-ScalePS(13))*0.5), 7*Scale, 13*Scale) imageName:@"jiantou_right_w"];
        [hrBgView addSubview:rightImgView];
    }
    UIButton *hrBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, StartY(hrHeaderImgView), WIDTH, ViewHeight(hrHeaderImgView)) imageName:nil target:self action:@selector(hrBtnTap) title:nil titleColor:nil];
    [hrBgView addSubview:hrBtn];
    
    UILabel *jobContentsTitleLbl = [UIToolsControl initLabelWithFrame:CGRectMake(LeftSpace, EndY(hrBgView)+15*Scale, 200, 55*Scale) text:@"职位描述" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_20];
    [self.shareBgView addSubview:jobContentsTitleLbl];
    
    UILabel *jobContentsLbl = [UIToolsControl initLabelWithFrame:CGRectMake(LeftSpace, EndY(jobContentsTitleLbl), WIDTH-30*Scale, model.contentsHeight) text:model.contents textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    jobContentsLbl.numberOfLines = 0;
    jobContentsLbl.lineBreakMode = NSLineBreakByWordWrapping;
    [self.shareBgView addSubview:jobContentsLbl];
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = LineSpacingHeight*2;
    jobContentsLbl.attributedText = [[NSAttributedString alloc] initWithString:model.contents attributes:@{NSFontAttributeName:Font(Font_13), NSParagraphStyleAttributeName:paraStyle}];
    
    CompanyTagsView *tagsBgView = [[CompanyTagsView alloc] initWithFrame:CGRectMake(LeftSpace, EndY(jobContentsLbl)+15*Scale, WIDTH-30*Scale, 110*Scale) dataArray:model.companyInfoModel.tags];
    [self.shareBgView addSubview:tagsBgView];
    [tagsBgView borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:(BorderSideTypeTop | BorderSideTypeBottom)];
    
    self.shareBgView.frame = CGRectMake(0, 0, WIDTH, EndY(tagsBgView)+80*Scale);
    
    self.shareBgImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(0, EndY(tagsBgView), WIDTH, 80*Scale) imageName:@"zhiweifenxiang_bg"];
    self.shareBgImgView.hidden = YES;
    [self.shareBgView addSubview:self.shareBgImgView];
//    UIImageView *leftImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(30*Scale, 12.5*Scale, 155*Scale, 55*Scale) imageName:@"zhiweifenxiang_logo"];
//    [self.shareBgImgView addSubview:leftImgView];
//    UIImageView *rightImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(WIDTH-30*Scale-57.5*Scale, 11.5*Scale, 57.5*Scale, 57.5*Scale) imageName:@"zhiweifenxiang_erweima"];
//    [self.shareBgImgView addSubview:rightImgView];
    
    CompanyContentView *companyContentsBgView = [[CompanyContentView alloc] initWithFrame:CGRectMake(LeftSpace, EndY(self.shareBgView)-80*Scale, WIDTH-30*Scale, 180*Scale) info:model.companyInfoModel target:self];
//    CompanyContentView *companyContentsBgView = [[CompanyContentView alloc] initWithFrame:CGRectMake(LeftSpace, EndY(tagsBgView), WIDTH-30*Scale, 180*Scale) info:model.companyInfoModel target:self];
    companyContentsBgView.backgroundColor = COLOR_OF_WHITE;
    [self addSubview:companyContentsBgView];
    [companyContentsBgView borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:(BorderSideTypeTop | BorderSideTypeBottom)];
    
    UIImageView *cautionIconImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(LeftSpace, EndY(companyContentsBgView)+15*Scale, 20*Scale, 16*Scale) imageName:@"caution_icon"];
    [self addSubview:cautionIconImgView];
    NSString *contactText = @"以担保或任何理由索取财务，扣押证件，均涉嫌违法，请提高警惕";
    CGFloat contactHeight = [UIToolsControl textHeightWithLineSpacing:contactText size:CGSizeMake(WIDTH-60*Scale, MAXFLOAT) font:Font_13]+1;
    UILabel *cautionLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(cautionIconImgView)+10*Scale, StartY(cautionIconImgView)-1, WIDTH-60*Scale, contactHeight) text:contactText textColor:THEMECOLOR_OF_ORANGE font:Font_13];
    [self addSubview:cautionLbl];
    NSMutableAttributedString *cautionAtt = [[NSMutableAttributedString alloc] initWithString:contactText];
    cautionLbl.attributedText = cautionAtt;
    cautionLbl.numberOfLines = 0;
    cautionLbl.lineBreakMode = NSLineBreakByWordWrapping;
    NSMutableParagraphStyle *cautionParaStyle = [[NSMutableParagraphStyle alloc] init];
    cautionParaStyle.lineSpacing = LineSpacingHeight;
    cautionLbl.attributedText = [[NSAttributedString alloc] initWithString:contactText attributes:@{NSFontAttributeName:Font(Font_13), NSParagraphStyleAttributeName:cautionParaStyle}];
    
    if (showFlag) {
        UILabel *bottomLbl = [UIToolsControl initLabelWithFrame:CGRectMake(0, EndY(cautionLbl)+10*Scale, WIDTH, 35*Scale) text:@"    同类职位" textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_13];
        bottomLbl.backgroundColor = THEMECOLOR_OF_GRAY;
        [self addSubview:bottomLbl];
    }
}
- (void)hrBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(jobDetailBaseInfoViewOfHRTap)]) {
        [self.delegate jobDetailBaseInfoViewOfHRTap];
    }
}
- (void)companyContentViewOfDetailTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(jobDetailBaseInfoViewOfCompanyDetailTap)]) {
        [self.delegate jobDetailBaseInfoViewOfCompanyDetailTap];
    }
}
- (void)companyContentViewOfAddressTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(jobDetailBaseInfoViewOfCompanyAddressTap)]) {
        [self.delegate jobDetailBaseInfoViewOfCompanyAddressTap];
    }
}

@end
